package deployr.core;

import static java.text.MessageFormat.*;

import java.util.*;

public class AppIdBuilder {

	
	private final Map<String, String> map = new HashMap<String, String>();
	
	private String separator = "-";

	public AppIdBuilder() {
		this("-");
	}

	public AppIdBuilder(String separator) {
		separateWith(separator);
	}

	public AppId build() {
		return new AppId(format("{0}{1}{2}", get("groupId").andConcat(separator), getRequired("artifactId").andConcat(separator), getRequired("version")));
	}

	private Option getRequired(String name) {
		return new Option((String) get(name).orThrow(format("{0} is required", name)));
	}

	private Option get(String key) {
		return new Option(map.get(key));
	}

	public AppIdBuilder groupId(String groupId) {
		map("groupId", groupId);
		return this;
	}

	private void map(String key, String value) {
		map.put(key, value);
	}

	public AppIdBuilder artifactId(String value) {
		map("artifactId", value);
		return this;
	}

	public AppIdBuilder version(String value) {
		map("version", value);
		return this;
	}

	private AppIdBuilder separateWith(String separator) {
		this.separator = separator;
		return this;
	}

}